%%人员
-module(fun_player).
-include("common.hrl").

-export([get_acc/1]).
-export([need_card/1]).
-export([check_card/2,spend_card/3,add_card/3]).
%%获取玩家信息
get_acc(Aid) ->
	case db:dirty_get(account, Aid) of
		[Acc | _] -> Acc;
		_ -> no
	end.
%%需要房卡数�need_card(#pt_create_csmj{times = 1}) -> 1;
need_card(#pt_create_csmj{times = 2}) -> 2;
need_card(#pt_create_zzmj{times = 1}) -> 1;
need_card(#pt_create_zzmj{times = 2}) -> 2;
need_card(#pt_create_jd_sg{room_rate = ?ROOM_OWNER_PAY}) -> 5;
need_card(#pt_create_jd_sg{room_rate = ?AA_PAY}) -> 1;
need_card(#pt_create_swsg{times = 1}) -> 1;
need_card(#pt_create_swsg{times = 2}) -> 2;
need_card(#pt_create_jhsg{times = 1}) -> 1;
need_card(#pt_create_jhsg{times = 2}) -> 2;
need_card(_) -> 1.
%%检查房卡数
check_card(Aid,CardNum) ->
	case db:dirty_get(account, Aid) of
		[#account{card = Card} | _] ->
			if
				Card >= CardNum -> true;
				true -> false
			end;
		_ -> false
	end.

%%消化房卡
spend_card(Sid,Aid,CardNum) when CardNum > 0 ->
	case db:dirty_get(account, Aid) of
		[Account = #account{card = Card} | _] ->
			CurCard = if
						  Card >= CardNum -> 
							  db:dirty_put(Account #account{card = Card - CardNum}),
							  Card - CardNum;
						  true -> 
							  db:dirty_put(Account #account{card = 0}),
							  0
					  end,
			Action = pt_action_int_d002:new(),
			Action1 = Action#pt_action_int{action = ?ACTION_CARD_CHG,data = CurCard}, 
			?send(Sid,pt_action_int_d002:to_binary(Action1));
		_ -> skip
	end;
spend_card(_Sid,_Aid,_CardNum) -> skip.
%%添加房卡
add_card(Sid,Aid,CardNum) when CardNum > 0 ->
	case db:dirty_get(account, Aid) of
		[Account = #account{} | _] -> 
			db:dirty_put(Account#account{card = Account#account.card + CardNum}),
			Action = pt_action_int_d002:new(),
			Action1 = Action#pt_action_int{action = ?ACTION_CARD_CHG,data = Account#account.card + CardNum},
			?send(Sid,pt_action_int_d002:to_binary(Action1));
		_ -> skip
	end;
add_card(_Sid,_Aid,_CardNum) -> skip.